#!/bin/bash
old_pgood=2
if [ ! -d /sys/bus/i2c/devices/i2c-1/1-0004 ]
then
	echo "there has no gpio driver"
	exit 
fi
reset_dc()
{
	devmem 0x32001468 32 0x0
	devmem 0x32001a18 32 0x0
	devmem 0x32001a18 32 0x18000111
}
while true
do
	pgood=`gpioget 7 1`
	if [ $old_pgood -ne $pgood ]
	then
		if [  $pgood -eq 1 ]
		then
			echo "change host state to running"
			busctl set-property xyz.openbmc_project.State.Host /xyz/openbmc_project/state/host0 xyz.openbmc_project.State.Host CurrentHostState s  xyz.openbmc_project.State.Host.HostState.Running
			busctl set-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis CurrentPowerState s xyz.openbmc_project.State.Chassis.PowerState.On
		elif [ $pgood -eq 0 ]
		then
			echo "change host state to power off"
			busctl set-property xyz.openbmc_project.State.Host /xyz/openbmc_project/state/host0 xyz.openbmc_project.State.Host CurrentHostState s  xyz.openbmc_project.State.Host.HostState.Off
			busctl set-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis CurrentPowerState s xyz.openbmc_project.State.Chassis.PowerState.Off
			#reset_dc
		else
			echo "unknown host status"
		fi
		old_pgood=$pgood
	fi
	sleep 3
done
